package miblab.school;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SetAdress extends Activity{

	protected static final String TAG = SetAdress.class.getSimpleName();
	private EditText mEdit1;
	private EditText mEdit2;
	private EditText mEdit3;
	private String home;
	private String school;
	private String hospital;
	private GeoPoint h;
	private GeoPoint ho;
	private GeoPoint sch;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setaddress);

		Button next = (Button) findViewById(R.id.button1);
		mEdit1   = (EditText)findViewById(R.id.editText1);
		mEdit2   = (EditText)findViewById(R.id.editText2);
		mEdit3   = (EditText)findViewById(R.id.EditText3);

		next.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				home = mEdit2.getText().toString();
				school = mEdit1.getText().toString();
				hospital = mEdit3.getText().toString();
				if(home!=null)
				{
					h=GetLoc(home);
					set(h,0);
				}
				if(hospital!=null)
				{
					ho=GetLoc(hospital);
					set(ho,1);
				}
				if(school!=null)
				{
					sch=GetLoc(school);
					set(sch,2);
				}
				Log.d(TAG,"Home"+h.getLatitudeE6()+h.getLongitudeE6()+"");
				Log.d(TAG,"Hospital"+ho.getLatitudeE6()+ho.getLongitudeE6()+"");
				Log.d(TAG,"School"+sch.getLatitudeE6()+sch.getLongitudeE6()+"");
				
				Intent intent = new Intent(SetAdress.this,ScreenSchoolActivity.class);
				startActivity(intent);
				Toast.makeText(SetAdress.this, "Changes Submitted", Toast.LENGTH_LONG).show();

			} 

		});
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
	@Override
	public void onBackPressed() {
		//Log.d("CDA", "onBackPressed Called");
		Intent intent = new Intent(SetAdress.this,ScreenSchoolActivity.class);
		startActivity(intent);
	}


	public GeoPoint GetLoc(String strAddress)
	{
		Geocoder coder = new Geocoder(this);
		List<Address> address;


		try {
			try {
				address = coder.getFromLocationName(strAddress,5);
				if (address == null) {
					return null;
				}
				Address location = address.get(0);
				location.getLatitude();
				location.getLongitude();

				GeoPoint p1 = new GeoPoint((int) (location.getLatitude() * 1E6),
						(int) (location.getLongitude() * 1E6));

				return p1;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}

	}
	
	
	//**********************Location Setting***********************************************
	public void set(GeoPoint  location,int locID)
	{
		double lat, lon;
		boolean status = false;

		try {
				
				

				if(location!=null)
				{
					Log.i(TAG,"LOCATION CHANGED"+location.getLatitudeE6()+"");
					Log.i(TAG,"LOCATION CHANGED"+location.getLongitudeE6() +"");
					lat = location.getLatitudeE6()/1E6;
					lon = location.getLongitudeE6()/1E6;
					
					switch(locID)
					{

					case 0:
					{

						status = SDwrite("Home.txt",lat,lon);
						break;
					}
					case 1:
					{

						status = SDwrite("School.txt",lat,lon);
						break;
					}
					case 2:
					{

						status = SDwrite("Hospital.txt",lat,lon);
						break;
					}

					}

					if(status)
						Toast.makeText(SetAdress.this, SystemParameters.LocName[locID]+"Location changed", Toast.LENGTH_LONG).show();
					else
					{
						Toast.makeText(SetAdress.this, "Check Internet", Toast.LENGTH_LONG).show();
					}

				}

				
		} catch (NullPointerException e1) { 
			Log.i(TAG,"Error 1");
			e1.printStackTrace();
		}
	}
	
	//**********************SD Write ******************************************************		
	public boolean SDwrite(String sFileName, double lat, double lon)
	{
		boolean state = false;
		try
		{
			File root = new File(SystemParameters.path, "School");
			if (!root.exists()) {
				root.mkdirs();
			}
			File gpxfile = new File(root,sFileName);
			FileWriter writer = new FileWriter(gpxfile);
			writer.write("Lat:"+lat+":Lon:"+lon+":Tim:"+0.0);
			writer.flush();
			writer.close();
			state = true;
		}
		catch(IOException e)
		{
			Toast.makeText(this, "Insert SD Card", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
			state = false;
		}

		return state;

	}

}
